package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.Const;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import com.frame.work.service.TblSecurityLogService;
import com.frame.work.service.impl.JpushService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 烟雾传感器0FAA8104
 */
@Data
public class YanWuChuanGanQi_0FAA8104 extends BaseFrame {

    private String switchState;

    public YanWuChuanGanQi_0FAA8104(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据

        String alarmState = StringUtils.substring(data, data.length() - 2, data.length());
        // String fangChaiState = StringUtils.substring(data,data.length() - 6, data.length() - 4);
        //数据库操作 begin
        String type = TypeEnum.NORMAL.code();
        if ("00".equals(this.getDataType())) {
            SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
            List<DeviceAssociatedInformation> list = testMapper.getDevice1(this.getDeviceMac());
            if (list.size() > 0) {
                DeviceAssociatedInformation deviceRelation = list.get(0);
                System.out.println(list.size());
                String homeName = "";
                if (StringUtils.isNotEmpty(deviceRelation.getHomeName())) {
                    homeName = deviceRelation.getHomeName();
                }
                String deviceName = deviceRelation.getDeviceName();
                if (deviceName == "" || deviceName == null) {
                    deviceName = "烟雾传感器";
                }
                String content = homeName + deviceName + "检测到烟雾浓度超标";
                type = TypeEnum.WARNING.code();
                /**
                 * 云片短信
                 */
                /**
                 * 极光推送
                 */
                String orgCode = deviceRelation.getOrganizationCode();
                String userId = deviceRelation.getUserId();
                String deviceId = deviceRelation.getDeviceId();
                String sourceId = deviceRelation.getSourceId();
                String sourceType = deviceRelation.getSourceType();

                if (StringUtils.isNotEmpty(sourceId)) {
                    JpushService jPushService = (JpushService) SpringUtil.getBean(JpushService.class);
                    try {
                        jPushService.sendNoSave("", content, type, sourceId, Const.MsgType.MSG_WARNING_TYPE, deviceId, userId, sourceType, orgCode);

                    } catch (Exception e) {

                    }
                }

                //保存报警信息
                TblSecurityLog tblSecurityLog = new TblSecurityLog();
                tblSecurityLog.setGatewayMac(this.getGatewayMac());
                tblSecurityLog.setDeviceMac(this.getDeviceMac());
                tblSecurityLog.setContent(content);
                tblSecurityLog.setType(type);

                tblSecurityLog.setUserId(userId);
                TblSecurityLogService tblSecurityLogService = (TblSecurityLogService) SpringUtil.getBean(TblSecurityLogService.class);
                tblSecurityLogService.insert(tblSecurityLog);
            }
        }

        //数据库操作 end

        this.setSwitchState(alarmState);

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", this.getDataType());
        actions.put("alarmState", alarmState);
        // actions.put("fangChaiState",fangChaiState);
        this.setActions(actions);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }

}
